<?php

Raise::load('core.RaiseObject');
Raise::load('core.asset.RaiseAssetType');
Raise::load('core.collection.RaiseCollection');
Raise::load('core.text.html.RaiseHtml');

/**
 * RaiseAsset class
 * An asset
 *
 * @author Sam Yong
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.core.asset
 * @since 1.2
 */
class RaiseAsset extends RaiseObject {
    
    /**
     * The unique identifier of this asset
     * @var string
     */
    private $name;
        
    /**
     * The client-side URL / path to the asset
     * @var string|RaiseUrl 
     */
    private $path;
    
    /**
     * The type of asset. Uses one of the constants from RaiseAssetType
     * @var string 
     */
    private $type;
    
    /**
     * The dependencies that this asset rely on.
     * @var RaiseCollection
     */
    private $dependencies;
    
    public function __construct($name, $path, $type, $dependencies = array()){
        $this->name = $name;
        $this->path = $path;
        $this->type = $type;
        if($dependencies == null){
            $dependencies = array();
        }
        $this->dependencies = new RaiseCollection($dependencies);
    }
    
    /**
     * Get or set the name / unique identifier of this asset
     * @param string $n (optional) Set the new name / unique identifier of this asset
     * @return string
     */
    public function name($n = null){
        if(func_num_args() == 1){
            $this->name = $n;
        }
        return $this->name;
    }

    /**
     * Get or set the client-side URL / path to the asset
     * @param string|RaiseUrl $p (optional) Set the client-side URL / path to the asset
     * @return string|RaiseUrl
     */
    public function path($p = null){
        if(func_num_args() == 1){
            $this->path = $p;
        }
        return $this->path;
    }

    /**
     * Get or set the type of asset. Uses one of the constants from RaiseAssetType
     * @param string $t (optional) Set the type of asset. Uses one of the constants from RaiseAssetType
     * @return string
     */
    public function type($t = null){
        if(func_num_args() == 1){
            $this->type = $t;
        }
        return $this->type;
    }

    /**
     * Get the dependencies that this asset rely on.
     * @return RaiseCollection
     */
    public function dependencies(){
        return $this->dependencies;
    }
    
    /**
     * Get the HTML output
     * @return string
     */
    public function output(){
        switch($this->type){
            case RaiseAssetType::CSS:
                return RaiseHtml::cssFile($this->path);
                break;
            case RaiseAssetType::JS:
                return RaiseHtml::scriptFile($this->path);
                break;
            case RaiseAssetType::FAVICON:
                //<link rel="shortcut icon" href="http://example.com/myicon.ico" />
                return RaiseHtml::tag('link', '', array('rel' => 'shortcut icon', 'href' => $this->path));
                break;
            case RaiseAssetType::RSS:
                //<link rel="alternate" type="application/rss+xml" title="RSS" href="url/to/rss/file">
                return RaiseHtml::tag('link', '', array('rel' => 'alternate', 'type' => 'application/rss+xml', 'title' => 'RSS', 'href' => $this->path));
                break;
        }
    }
    
}